package cc.mrbird.assets.controller;

import cc.mrbird.assets.domain.Layer;
import cc.mrbird.assets.service.LayerService;
import cc.mrbird.assets.service.LayerService;
import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(description = "层号管理接口")
@Controller
public class LayerController extends BaseController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LayerService layerService;
    @Log("获取层号信息")
    @RequestMapping(value = "layer",method = RequestMethod.POST)
    /* @RequiresPermissions("layer:list")*/
    public String index() {
        return "assets/layer/layer";
    }
    @Log("获取层号信息")
    @ApiOperation(value = "获取层号信息" ,  notes="获取层号信息")
    @RequestMapping(value = "layer/list",method = RequestMethod.POST)
    /*@RequiresPermissions("layer:list")*/
    @ResponseBody
    public Map<String, Object> layerList(QueryRequest request, Layer layer) {
        return super.selectByPageNumSize(request, () -> this.layerService.findAllList(layer, null));
    }
    @ApiOperation(value = "层号信息添加")
    @RequestMapping(value = "layer/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo add(Layer layer) {
        try {
            Layer result = this.layerService.findByName(layer.getLayer_name());
            if (result == null) {
                this.layerService.add(layer);
            }
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }
    @ApiOperation(value = "修改层号信息")
    @Log("修改层号信息")
    /* @RequiresPermissions("layer:update")*/
    @RequestMapping(value = "layer/update" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo update(Layer layer) {
        try {
            this.layerService.update(layer);
            return ResponseBo.ok("修改层号信息成功！");
        } catch (Exception e) {
            log.error("修改层号信息失败", e);
            return ResponseBo.error("修改层号信息失败，请联系网站管理员！");
        }
    }
    @Log("删除层号信息")
    /*@RequiresPermissions("layer:delete")*/
    @RequestMapping(value = "layer/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo delete(String ids) {
        try {
            this.layerService.delete(ids);
            return ResponseBo.ok("删除层号信息成功！");
        } catch (Exception e) {
            log.error("删除层号信息失败", e);
            return ResponseBo.error("删除层号信息失败，请联系网站管理员！");
        }
    }
}
